﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace ExampleCallWs
{
    public partial class DangNhap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnDangNhap_Click(object sender, EventArgs e)
        {
            try
            {
                ProxyKD11.Url = "http://www.kd11.somee.com/ws/KD11.asmx";
                string sid = ProxyKD11.Login(txtTenDangNhap.Text, txtMatKhau.Text);
                if (String.IsNullOrEmpty(sid))
                {
                    lbLoi.Text = "Đăng nhập thất bại";
                }
                else
                {
                    Session["sid"] = sid;
                    pnDangNhap.Visible = false;
                    pnLayDanhSachSanPham.Visible = true;
                    pnDonHang.Visible = true;
                }
            }
            catch (Exception ex)
            {
                lbChiTiet.Text = ex.Message;
                lbLoi.Text = "Kiểm tra tên đăng nhập và mật khẩu";
            }
        }

        protected void btnLayDanhSachSanPham_Click(object sender, EventArgs e)
        {
            try
            {
                ProxyKD11.Url = "http://www.kd11.somee.com/ws/KD11.asmx";
                string json = ProxyKD11.LayDanhSachSPJson((string)Session["sid"]);
                JArray j = JArray.Parse(json);
                JsonSerializer serializer = new JsonSerializer();
                List<wsSanPham> ds = serializer.Deserialize<List<wsSanPham>>(new JTokenReader(j));
                grdDanhSachSanPham.DataSource = ds;
                grdDanhSachSanPham.DataBind();
            }
            catch (Exception ex)
            {

                lbChiTiet.Text = ex.Message;
            }

        }

        protected void btnLayThongTinDonHang_Click(object sender, EventArgs e)
        {
            // Dùng tiện ích bên dưới để sử lý với kết quả trả về ở dạng XML
            // Utility.DeserializeObject<wsDonDatHang>(xml);
            try
            {
                ProxyKD11.Url = "http://www.kd11.somee.com/ws/KD11.asmx";
                string json = ProxyKD11.LayThongTinDonHang((string)Session["sid"]);
                JArray j = JArray.Parse(json);
                JsonSerializer serializer = new JsonSerializer();
                List<wsDonDatHang> ds = serializer.Deserialize<List<wsDonDatHang>>(new JTokenReader(j));
                grdDonHang.DataSource = ds;
                grdDonHang.DataBind();
            }
            catch (Exception ex)
            {

                lbChiTiet.Text = ex.Message;
            }


        }

        protected void lnkThoat_Click(object sender, EventArgs e)
        {
            ProxyKD11.Url = "http://www.kd11.somee.com/ws/KD11.asmx";
            ProxyKD11.Logout((string)Session["sid"]);
            Session["sid"] = null;
            pnDangNhap.Visible = true;            
            pnDonHang.Visible = false;
            pnLayDanhSachSanPham.Visible = false;
        }
    }
}